package yto.freemarker.springaop.factory;

import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.Proxy;

/**
 * @author dongxing
 **/
public class MyFactoryBean implements FactoryBean {

    public MyFactoryBean() {
    }

    Class aClass;
    public MyFactoryBean(Class aClass) {
        this.aClass = aClass;
    }
    /**
     * factorybean的getObject返回的是代理对象，而不是factorybean本身
     *
     * @return
     * @throws Exception
     */
    @Override
    public Object getObject() throws Exception {
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),new Class[]{aClass},(x,y,z)->{
            System.out.println("动态代理！！");
            System.out.println(y.getAnnotation(Select.class).value()[0]);
//            return y.invoke(aClass, z);
            return null;
        });
    }

    @Override
    public Class<?> getObjectType() {
        return aClass;
    }
}
